﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.Drawing;
using MNS.Activities.Common;

namespace MNS.Activities.Editors.TimeEditor
{
    public class TimeUIEditorType : UITypeEditor
    {
        //public override void PaintValue(PaintValueEventArgs e)
        //{
        //    DateTime dt = (DateTime)e.Value;
        //    e.Graphics.DrawString("asd", new Font("Arial", 10.0f), Brushes.Red, e.Bounds);
        //}

        //public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        //{
        //    return true;
        //}

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null || context.Instance == null)
                return base.GetEditStyle(context);
            
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService;

            if (context == null || context.Instance == null || provider == null)
                return value;

            try
            {
                Time result = Time.Default;
                
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                TimeEditorDialog dlg = new TimeEditorDialog();
                
                dlg.PropertyValue = (Time)value;
                using (dlg)
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        result = dlg.PropertyValue;
                    }
                    else
                    {
                        if ((value as Time) != null)
                            result = value as Time;
                    }
                }
                return result;

            }
            finally
            {
                editorService = null;
            }
        }
    }
}
